<template>
  <div class="page-register">
    <article class="header">
      <header>
        <a
          href="/"
          class="site-logo"/>
        <span class="login">
          <em class="bold">已有美团账号？</em>
          <a href="/login">
            <el-button
              type="primary"
              size="small">登录</el-button>
          </a>
        </span>
      </header>
    </article>
    <section>
      <el-form
        ref="ruleForm"
        :model="ruleForm"
        :rules="rules"
        label-width="100px"
        class="demo-ruleForm">
        <el-form-item
          label="用户名"
          prop="name">
          <el-input v-model="ruleForm.name"/>
        </el-form-item>
        <el-form-item
          label="邮箱"
          prop="email">
          <el-input v-model="ruleForm.email"/>
          <el-button
            size="mini"
            round
            @click="sendMsg">发送验证码
          </el-button>
          <span class="status">{{ statusMsg }}</span>
        </el-form-item>
        <el-form-item
          label="验证码"
          prop="code"
        >
          <el-input
            v-model="ruleForm.code"
            maxlength="4"/>
        </el-form-item>
        <el-form-item
          label="密码"
          prop="pwd"
        >
          <el-input
            v-model="ruleForm.pwd"
            type="password"/>
        </el-form-item>
        <el-form-item
          label="确认密码"
          prop="cpwd"
        >
          <el-input
            v-model="ruleForm.cpwd"
            type="password"/>
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            @click="register">同意以下协议并注册
          </el-button>
          <div class="error">{{ error }}</div>
        </el-form-item>
        <el-form-item>
          <a
            class="f1"
            href="http://www.meituan.com/about/terms"
            target="_blank">《美团网用户协议》</a>
        </el-form-item>
      </el-form>
    </section>
  </div>
</template>

<script>
/* eslint-disable prettier/prettier */
import { MD5 } from '../util'

export default {
  layout: 'blank',
  data() {
    return {
      statusMsg: '',
      error: '',
      ruleForm: {
        name: '',
        email: '',
        code: '',
        pwd: '',
        cpwd: ''
      },
      rules: {
        name: [
          {
            required: true,
            type: 'string',
            message: '请输入用户名',
            trigger: 'blur'
          }
        ],
        email: [
          {
            required: true,
            type: 'email',
            message: '请输入邮箱',
            trigger: 'blur'
          }
        ],
        code: [
          {
            required: true,
            type: 'string',
            message: '请输入验证码',
            trigger: 'blur'
          }
        ],
        pwd: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          }
        ],
        cpwd: [
          {
            required: true,
            message: '请输入密码',
            trigger: 'blur'
          },
          {
            validator: (rule, value, callback) => {
              !value ? callback(new Error('请再次输入密码'))
                : (value !== this.ruleForm.pwd ? callback('两次输入密码不一致') : callback())
              console.log('validator this', this)
            },
            trigger: 'blur'
          }
        ]
      }
    }
  },
  methods: {
    async sendMsg() {
      clearInterval(this.timer)
      let nameValid, emailValid
      const ruleForm = this.$refs['ruleForm']
      ruleForm.validateField('name', valid => (nameValid = valid))
      ruleForm.validateField('email', valid => (emailValid = valid))
      if (nameValid || emailValid) {
        return false
      }
      this.statusMsg = ''
      const {status, data} = await this.$axios.post('/user/verify', {
        username: window.encodeURIComponent(this.ruleForm.name),
        email: this.ruleForm.email
      })
      let count = 60 * 3
      if (status !== 200 || data.code !== 0) {
        return this.statusMsg = data.msg
      }
      this.timer = setInterval(() => {
        if (count === 0) {
          this.statusMsg = ''
          return clearInterval(this.timer)
        }
        this.statusMsg = `验证码已发送，剩余${count--}秒`
      }, 1000)
    },
    register() {
      this.$refs['ruleForm'].validate(async valid => {
        if (valid) {
          const {name, pwd, email, code} = this.ruleForm
          const {status, data} = await this.$axios.post('/user/signup', {
            username: window.encodeURIComponent(name),
            password: MD5(pwd),
            email,
            code
          })
          if (status != 200) {
            return this.error = '服务器出错'
          }
          if (data && data.code != 0) {
            return this.error = data.msg
          }
          location.href = '/login'
          setTimeout(() => {
            this.error = ''
          }, 1500)
        }
      })
    }
  }
}
</script>

<style lang="scss">
@import '~/assets/css/register/index.scss';
</style>
